
#include "PolicyMenu.h"

BString PolicyMenu::PolicyText(Policy policy)
{
	BString text;
	if (policy == Policy_None)
	{
		text = "None";
	}
	else
	{
		if (policy & Policy_Fixed)
			text << "Fixed";
		if (policy & Policy_Preferred)
			text << "Preferred";
		if (policy & Policy_Expanding)
			text << "Expanding";
		if (policy & Policy_Shrinking)
			text << "Shrinking";
	}
	return text;
}

PolicyMenu::PolicyMenu(BWidget* parent) : BMenu(parent)
{
	this->addAction("None")->setAsset(Policy_None);
	this->addAction("Fixed")->setAsset(Policy_Fixed);
	this->addAction("Preferred")->setAsset(Policy_Preferred);
	this->addAction("Shrinking")->setAsset(Policy_Shrinking);
	this->addAction("Expanding")->setAsset(Policy_Expanding);
	for (int i = 0; i < this->count(); i++)
	{
		this->action(i)->setCheckable(true);
	}
	connect(this, Signal_Checked, &PolicyMenu::slotActionChecked);
}

void PolicyMenu::setPolicy(Policy policy)
{
	_policy = policy;
	for (int i = 0; i < this->count(); i++)
	{
		BAction* action = this->action(i);
		PolicyFlag p = action->asset();
		action->setChecked(_policy & p);
	}
	BString title = PolicyText(_policy);
	this->setTitle(title);
}
Policy PolicyMenu::policy()
{
	return _policy;
}

void PolicyMenu::slotActionChecked()
{
	Policy policy = Policy_None;
	for (int i = 0; i < this->count(); i++)
	{
		BAction* action = this->action(i);
		if (action->checked())
		{
			PolicyFlag p = action->asset();
			policy |= p;
		}
	}
	this->setPolicy(policy);
}


